﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Company.BLL.WS.KDT.GC;

namespace Company.BLL.KDT.GC
{
	public partial class HopDong
	{
		#region Private members.
		
		protected long _ID;
		protected long _SoTiepNhan;
		protected int _TrangThaiXuLy;
		protected string _SoHopDong = string.Empty;
		protected string _MaHaiQuan = string.Empty;
		protected string _MaDoanhNghiep = string.Empty;
		protected string _MaDaiLy = string.Empty;
		protected DateTime _NgayKy;
		protected DateTime _NgayDangKy;
		protected DateTime _NgayHetHan;
		protected DateTime _NgayGiaHan;
		protected decimal _TriGiaGiaCong;
		protected string _NuocThue_ID = string.Empty;
		protected string _NguyenTe_ID = string.Empty;
        protected DateTime _NgayTiepNhan;
        protected int _isOnServer=0;
		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public long ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}

        public int isOnServer
        {
            set { this._isOnServer = value; }
            get { return this._isOnServer; }
        }

		public long SoTiepNhan
		{
			set {this._SoTiepNhan = value;}
			get {return this._SoTiepNhan;}
		}
		
		public int TrangThaiXuLy
		{
			set {this._TrangThaiXuLy = value;}
			get {return this._TrangThaiXuLy;}
		}
		
		public string SoHopDong
		{
			set {this._SoHopDong = value;}
			get {return this._SoHopDong;}
		}
		
		public string MaHaiQuan
		{
			set {this._MaHaiQuan = value;}
			get {return this._MaHaiQuan;}
		}
		
		public string MaDoanhNghiep
		{
			set {this._MaDoanhNghiep = value;}
			get {return this._MaDoanhNghiep;}
		}
		
		public string MaDaiLy
		{
			set {this._MaDaiLy = value;}
			get {return this._MaDaiLy;}
		}
		
		public DateTime NgayKy
		{
			set {this._NgayKy = value;}
			get {return this._NgayKy;}
		}

        public DateTime NgayTiepNhan
        {
            set { this._NgayTiepNhan = value; }
            get { return this._NgayTiepNhan; }
        }

		public DateTime NgayDangKy
		{
			set {this._NgayDangKy = value;}
			get {return this._NgayDangKy;}
		}
		
		public DateTime NgayHetHan
		{
			set {this._NgayHetHan = value;}
			get {return this._NgayHetHan;}
		}
		
		public DateTime NgayGiaHan
		{
			set {this._NgayGiaHan = value;}
			get {return this._NgayGiaHan;}
		}
		
		public decimal TriGiaGiaCong
		{
			set {this._TriGiaGiaCong = value;}
			get {return this._TriGiaGiaCong;}
		}
		
		public string NuocThue_ID
		{
			set {this._NuocThue_ID = value;}
			get {return this._NuocThue_ID;}
		}
		
		public string NguyenTe_ID
		{
			set {this._NguyenTe_ID = value;}
			get {return this._NguyenTe_ID;}
		}
		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static HopDong Load(long iD)
		{
			string spName = "[dbo].p_KDT_GC_HopDong_Load";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, iD);
			HopDong entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new HopDong();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayDangKy"))) entity.NgayDangKy = reader.GetDateTime(reader.GetOrdinal("NgayDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayHetHan"))) entity.NgayHetHan = reader.GetDateTime(reader.GetOrdinal("NgayHetHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayGiaHan"))) entity.NgayGiaHan = reader.GetDateTime(reader.GetOrdinal("NgayGiaHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TriGiaGiaCong"))) entity.TriGiaGiaCong = reader.GetDecimal(reader.GetOrdinal("TriGiaGiaCong"));
				if (!reader.IsDBNull(reader.GetOrdinal("NuocThue_ID"))) entity.NuocThue_ID = reader.GetString(reader.GetOrdinal("NuocThue_ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe_ID = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
			}
			reader.Close();
			
			
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------

        public static HopDong LoadBySoTiepNhan(long sotiepnhan)
        {
            string sql = "Select * from t_KDT_GC_HopDong where SoTiepNhan=@SoTiepNhan";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(sql);

            db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, sotiepnhan);
            HopDong entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new HopDong();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayDangKy"))) entity.NgayDangKy = reader.GetDateTime(reader.GetOrdinal("NgayDangKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayHetHan"))) entity.NgayHetHan = reader.GetDateTime(reader.GetOrdinal("NgayHetHan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayGiaHan"))) entity.NgayGiaHan = reader.GetDateTime(reader.GetOrdinal("NgayGiaHan"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGiaGiaCong"))) entity.TriGiaGiaCong = reader.GetDecimal(reader.GetOrdinal("TriGiaGiaCong"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocThue_ID"))) entity.NuocThue_ID = reader.GetString(reader.GetOrdinal("NuocThue_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe_ID = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        public static HopDong Load(string soHopDong)
        {
            string sql = "Select * from t_KDT_GC_HopDong where SoHopDong=@SoHopDong";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(sql);

            db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, soHopDong);
            HopDong entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new HopDong();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayDangKy"))) entity.NgayDangKy = reader.GetDateTime(reader.GetOrdinal("NgayDangKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayHetHan"))) entity.NgayHetHan = reader.GetDateTime(reader.GetOrdinal("NgayHetHan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayGiaHan"))) entity.NgayGiaHan = reader.GetDateTime(reader.GetOrdinal("NgayGiaHan"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGiaGiaCong"))) entity.TriGiaGiaCong = reader.GetDecimal(reader.GetOrdinal("TriGiaGiaCong"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocThue_ID"))) entity.NuocThue_ID = reader.GetString(reader.GetOrdinal("NuocThue_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe_ID = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }		

		//---------------------------------------------------------------------------------------------
		


		public static DataSet SelectAll()
        {
            string spName = "[dbo].p_KDT_GC_HopDong_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_KDT_GC_HopDong_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static HopDongCollection SelectCollectionAll()
		{
			HopDongCollection collection = new HopDongCollection();

			IDataReader reader = SelectReaderAll();
			while (reader.Read())
			{
				HopDong entity = new HopDong();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayDangKy"))) entity.NgayDangKy = reader.GetDateTime(reader.GetOrdinal("NgayDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayHetHan"))) entity.NgayHetHan = reader.GetDateTime(reader.GetOrdinal("NgayHetHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayGiaHan"))) entity.NgayGiaHan = reader.GetDateTime(reader.GetOrdinal("NgayGiaHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TriGiaGiaCong"))) entity.TriGiaGiaCong = reader.GetDecimal(reader.GetOrdinal("TriGiaGiaCong"));
				if (!reader.IsDBNull(reader.GetOrdinal("NuocThue_ID"))) entity.NuocThue_ID = reader.GetString(reader.GetOrdinal("NuocThue_ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe_ID = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_HopDong_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_HopDong_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static HopDongCollection SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			HopDongCollection collection = new HopDongCollection();

			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				HopDong entity = new HopDong();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoHopDong"))) entity.SoHopDong = reader.GetString(reader.GetOrdinal("SoHopDong"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) entity.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) entity.MaDaiLy = reader.GetString(reader.GetOrdinal("MaDaiLy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayKy"))) entity.NgayKy = reader.GetDateTime(reader.GetOrdinal("NgayKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayDangKy"))) entity.NgayDangKy = reader.GetDateTime(reader.GetOrdinal("NgayDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayHetHan"))) entity.NgayHetHan = reader.GetDateTime(reader.GetOrdinal("NgayHetHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayGiaHan"))) entity.NgayGiaHan = reader.GetDateTime(reader.GetOrdinal("NgayGiaHan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TriGiaGiaCong"))) entity.TriGiaGiaCong = reader.GetDecimal(reader.GetOrdinal("TriGiaGiaCong"));
				if (!reader.IsDBNull(reader.GetOrdinal("NuocThue_ID"))) entity.NuocThue_ID = reader.GetString(reader.GetOrdinal("NuocThue_ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe_ID = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}


		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.

        public static long Insert(long soTiepNhan, int trangThaiXuLy, string soHopDong, string maHaiQuan, string maDoanhNghiep, string maDaiLy, DateTime ngayKy, DateTime ngayDangKy, DateTime ngayHetHan, DateTime ngayGiaHan, decimal triGiaGiaCong, string nuocThue_ID, string nguyenTe_ID, DateTime ngayTiepNhan)
		{
			HopDong entity = new HopDong();			
			entity.SoTiepNhan = soTiepNhan;
			entity.TrangThaiXuLy = trangThaiXuLy;
			entity.SoHopDong = soHopDong;
			entity.MaHaiQuan = maHaiQuan;
			entity.MaDoanhNghiep = maDoanhNghiep;
			entity.MaDaiLy = maDaiLy;
			entity.NgayKy = ngayKy;
			entity.NgayDangKy = ngayDangKy;
			entity.NgayHetHan = ngayHetHan;
			entity.NgayGiaHan = ngayGiaHan;
			entity.TriGiaGiaCong = triGiaGiaCong;
			entity.NuocThue_ID = nuocThue_ID;
			entity.NguyenTe_ID = nguyenTe_ID;
            entity.NgayTiepNhan = ngayTiepNhan;
			long returnID = entity.Insert();
						
			return returnID;
		}
		
		public bool Insert(HopDongCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HopDong item in collection)
                    {
                        if (item.InsertTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}
		
		public long Insert()
		{
			return this.InsertTransaction(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public long InsertTransaction(SqlTransaction transaction)
		{			
			string spName = "[dbo].p_KDT_GC_HopDong_Insert";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.BigInt, 8);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
			db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._TrangThaiXuLy);
			db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, this._MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
			db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._MaDaiLy);
			db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
			db.AddInParameter(dbCommand, "@NgayDangKy", SqlDbType.DateTime, this._NgayDangKy);
			db.AddInParameter(dbCommand, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
			db.AddInParameter(dbCommand, "@NgayGiaHan", SqlDbType.DateTime, this._NgayGiaHan);
			db.AddInParameter(dbCommand, "@TriGiaGiaCong", SqlDbType.Money, this._TriGiaGiaCong);
			db.AddInParameter(dbCommand, "@NuocThue_ID", SqlDbType.Char, this._NuocThue_ID);
			db.AddInParameter(dbCommand, "@NguyenTe_ID", SqlDbType.Char, this._NguyenTe_ID);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				this._ID = (long) db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				this._ID = (long) db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}			
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		public int InsertUpdate()
		{
			return this.InsertUpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdateTransaction(SqlTransaction transaction)
		{			
			string spName = "p_KDT_GC_HopDong_InsertUpdate";	
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
			db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._TrangThaiXuLy);
			db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, this._MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
			db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._MaDaiLy);
			db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
			db.AddInParameter(dbCommand, "@NgayDangKy", SqlDbType.DateTime, this._NgayDangKy);
			db.AddInParameter(dbCommand, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
			db.AddInParameter(dbCommand, "@NgayGiaHan", SqlDbType.DateTime, this._NgayGiaHan);
			db.AddInParameter(dbCommand, "@TriGiaGiaCong", SqlDbType.Money, this._TriGiaGiaCong);
			db.AddInParameter(dbCommand, "@NuocThue_ID", SqlDbType.Char, this._NuocThue_ID);
			db.AddInParameter(dbCommand, "@NguyenTe_ID", SqlDbType.Char, this._NguyenTe_ID);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);
            db.AddOutParameter(dbCommand, "@IDOut", SqlDbType.BigInt, 8);
            int n = 0;
            if (transaction != null)
                n = db.ExecuteNonQuery(dbCommand, transaction);
            else
                n = db.ExecuteNonQuery(dbCommand);
            this._ID = (long)db.GetParameterValue(dbCommand, "@IDOut");
            return n;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public bool InsertUpdate(HopDongCollection collection)
        {
            bool ret;			
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HopDong item in collection)
                    {
                        if (item.InsertUpdateTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

        public bool InsertUpdateHopDong(NguyenPhuLieuCollection npl, SanPhamCollection sp, NhomSanPhamCollection nhomsp, ThietBiCollection tb)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    bool ret01 = true;
                    long count = InsertUpdateTransaction(transaction);
                    if (count > 0)
                    foreach (NguyenPhuLieu item in npl)
                    {
                        item.HopDong_ID = this._ID;
                        if (item.InsertUpdateTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    foreach (NhomSanPham item in nhomsp)
                    {
                        item.HopDong_ID = this._ID;
                        if (item.InsertUpdateTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    foreach (SanPham item in sp)
                    {
                        item.HopDong_ID = this._ID;
                        if (item.InsertUpdateTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    foreach (ThietBi item in tb)
                    {
                        item.HopDong_ID = this._ID;
                        if (item.InsertUpdateTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    if (ret01)
                    {
                        transaction.Commit();
                        ret = true;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = false;
                    }
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;	
        }
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int Update(long iD, long soTiepNhan, int trangThaiXuLy, string soHopDong, string maHaiQuan, string maDoanhNghiep, string maDaiLy, DateTime ngayKy, DateTime ngayDangKy, DateTime ngayHetHan, DateTime ngayGiaHan, decimal triGiaGiaCong, string nuocThue_ID, string nguyenTe_ID)
		{
			HopDong entity = new HopDong();			
			entity.ID = iD;
			entity.SoTiepNhan = soTiepNhan;
			entity.TrangThaiXuLy = trangThaiXuLy;
			entity.SoHopDong = soHopDong;
			entity.MaHaiQuan = maHaiQuan;
			entity.MaDoanhNghiep = maDoanhNghiep;
			entity.MaDaiLy = maDaiLy;
			entity.NgayKy = ngayKy;
			entity.NgayDangKy = ngayDangKy;
			entity.NgayHetHan = ngayHetHan;
			entity.NgayGiaHan = ngayGiaHan;
			entity.TriGiaGiaCong = triGiaGiaCong;
			entity.NuocThue_ID = nuocThue_ID;
			entity.NguyenTe_ID = nguyenTe_ID;
			return entity.Update();
		}
		
		public int Update()
		{
			return this.UpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool Update(HopDongCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HopDong item in collection)
                    {
                        if (item.UpdateTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		public int UpdateTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_HopDong_Update";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
			db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._TrangThaiXuLy);
			db.AddInParameter(dbCommand, "@SoHopDong", SqlDbType.VarChar, this._SoHopDong);
			db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, this._MaHaiQuan);
			db.AddInParameter(dbCommand, "@MaDoanhNghiep", SqlDbType.VarChar, this._MaDoanhNghiep);
			db.AddInParameter(dbCommand, "@MaDaiLy", SqlDbType.VarChar, this._MaDaiLy);
			db.AddInParameter(dbCommand, "@NgayKy", SqlDbType.DateTime, this._NgayKy);
			db.AddInParameter(dbCommand, "@NgayDangKy", SqlDbType.DateTime, this._NgayDangKy);
			db.AddInParameter(dbCommand, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
			db.AddInParameter(dbCommand, "@NgayGiaHan", SqlDbType.DateTime, this._NgayGiaHan);
			db.AddInParameter(dbCommand, "@TriGiaGiaCong", SqlDbType.Money, this._TriGiaGiaCong);
			db.AddInParameter(dbCommand, "@NuocThue_ID", SqlDbType.Char, this._NuocThue_ID);
			db.AddInParameter(dbCommand, "@NguyenTe_ID", SqlDbType.Char, this._NguyenTe_ID);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int Delete(long iD)
		{
			HopDong entity = new HopDong();
			entity.ID = iD;
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.DeleteTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool DeleteCollection(HopDongCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HopDong item in collection)
                    {
                        if (item.DeleteTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		//---------------------------------------------------------------------------------------------

		public static void DeleteCollection(HopDongCollection collection, SqlTransaction transaction)
		{			
            foreach (HopDong item in collection)
            {
                item.DeleteTransaction(transaction);
            }			
		}

		public int DeleteTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_HopDong_Delete";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		#endregion

        #region WEBSERVICE
        // khai bao mot hop dong
        public HopDongInfo ConvertToHopDongInfo()
        {
            #region Hợp đồng
            HopDongInfo hdInfo = new HopDongInfo();
            hdInfo.MaDaiLy = this._MaDaiLy;
            hdInfo.MaDoanhNghiep = this._MaDoanhNghiep;
            hdInfo.MaHaiQuan = this._MaHaiQuan;
            hdInfo.NgayDangKy = this._NgayDangKy;
            hdInfo.NgayGiaHan = this._NgayGiaHan;
            hdInfo.NgayHetHan = this._NgayHetHan;
            hdInfo.NgayKy = this._NgayKy;
            hdInfo.NamTiepNhan = this.NgayTiepNhan.Year;
            hdInfo.NgayTiepNhan = this.NgayTiepNhan;
            hdInfo.NguyenTe_ID = this.NguyenTe_ID;
            hdInfo.NuocThue_ID = this.NuocThue_ID;
            hdInfo.SoHopDong = this.SoHopDong;
            hdInfo.SoTiepNhan = this.SoTiepNhan;
            hdInfo.TrangThaiXL = this.TrangThaiXuLy;
            hdInfo.TriGiaGiaCong = this.TriGiaGiaCong;

            #endregion Hợp đồng

            #region Nhóm sản phẩm
            NhomSanPhamCollection nhomspCollection = NhomSanPham.SelectCollectionBy_HopDong_ID(this.ID);
            int i = 0;
            hdInfo.NhomSanPhamGiaCong = new NhomSanPhamInfo[nhomspCollection.Count];
            foreach (NhomSanPham nhomsp in nhomspCollection)
            {
                NhomSanPhamInfo nhomspInfo = new NhomSanPhamInfo();
                nhomspInfo.DVGC = this.MaDoanhNghiep;
                nhomspInfo.GiaGiaCong = nhomsp.GiaGiaCong;
                nhomspInfo.MaHaiQuan = this.MaHaiQuan;
                nhomspInfo.MaNhomSP = nhomsp.MaSanPham;
                nhomspInfo.NgaykyHopDong = this.NgayKy;
                nhomspInfo.SoHopDong = this.SoHopDong;
                nhomspInfo.SoLuong = nhomsp.SoLuong;
                nhomspInfo.STTHang = nhomsp.STTHang;
                Company.BLL.DuLieuChuan.NhomSanPham nhomdlc = new Company.BLL.DuLieuChuan.NhomSanPham();
                nhomspInfo.MaDuKien = nhomdlc.getDuKien(nhomsp.MaSanPham);
                hdInfo.NhomSanPhamGiaCong[i++] = nhomspInfo;
            }
            #endregion Nhóm sản phẩm

            #region sản phẩm
            i = 0;
            SanPhamCollection spCollection = SanPham.SelectCollectionBy_HopDong_ID(this.ID);
            hdInfo.SanPhamCollection = new SanPhamInfo[spCollection.Count];
            foreach (SanPham sp in spCollection)
            {
                SanPhamInfo spInfo = new SanPhamInfo();
                spInfo.DVT_ID = sp.DVT_ID;
                spInfo.Ma = sp.Ma;
                spInfo.MaDoanhNghiep = this.MaDoanhNghiep;
                spInfo.MaHaiQuan = this.MaHaiQuan;
                spInfo.MaHS = sp.MaHS;
                spInfo.NgayKy = this.NgayKy;
                spInfo.SoHopDong = this.SoHopDong;
                spInfo.SoLuongDangKy = sp.SoLuongDangKy;
                spInfo.STTHang = sp.STTHang;
                spInfo.Ten = sp.Ten;
                spInfo.NhomSanPhamGiaCong = NhomSanPham.Load(sp.NhomSanPham_ID).MaSanPham;
                hdInfo.SanPhamCollection[i++] = spInfo;
            }
            #endregion  sản phẩm

            #region Nguyên phụ liệu
            NguyenPhuLieuCollection nplCollection = NguyenPhuLieu.SelectCollectionBy_HopDong_ID(this.ID);
            i = 0;
            hdInfo.NguyenPhuLieuCollection = new NguyenPhuLieuInfo[nplCollection.Count];
            foreach (NguyenPhuLieu npl in nplCollection)
            {
                NguyenPhuLieuInfo nplInfo = new NguyenPhuLieuInfo();
                nplInfo.DVT_ID = npl.DVT_ID;
                nplInfo.Ma = npl.Ma;
                nplInfo.MaDoanhNghiep = this.MaDoanhNghiep;
                nplInfo.MaHaiQuan = this.MaHaiQuan;
                nplInfo.MaHS = npl.MaHS;
                nplInfo.NgayKy = this.NgayKy;
                nplInfo.SoHopDong = this.SoHopDong;
                nplInfo.SoLuongDangKy = npl.SoLuongDangKy;
                nplInfo.STTHang = npl.STTHang;
                nplInfo.Ten = npl.Ten;
                hdInfo.NguyenPhuLieuCollection[i++] = nplInfo;
            }

            #endregion Nguyên phụ liệu

            #region Thiết bị
            ThietBiCollection tbCollection = ThietBi.SelectCollectionBy_HopDong_ID(this.ID);
            i = 0;
            hdInfo.ThietBiCollection = new ThietBiInfo[tbCollection.Count];
            foreach (ThietBi tb in tbCollection)
            {
                ThietBiInfo tbInfo = new ThietBiInfo();
                tbInfo.DonGia = tb.DonGia;
                tbInfo.DVT_ID = tb.DVT_ID;
                tbInfo.Ma = tb.Ma;
                tbInfo.MaDoanhNghiep = this.MaDoanhNghiep;
                tbInfo.MaHaiQuan = this.MaHaiQuan;
                tbInfo.MaHS = tb.MaHS;
                tbInfo.NgayKy = this.NgayKy;
                tbInfo.NguyenTe_ID = tb.NguyenTe_ID;
                tbInfo.NuocXX_ID = tb.NuocXX_ID;
                tbInfo.SoHopDong = this.SoHopDong;
                tbInfo.SoLuong = tb.SoLuongDangKy;
                tbInfo.STTHang = tb.STTHang;
                tbInfo.Ten = tb.Ten;
                tbInfo.TinhTrang = tb.TinhTrang;
                tbInfo.TriGia = tb.TriGia;
                hdInfo.ThietBiCollection[i++] = tbInfo;
            }

            #endregion Thiết bị
            return hdInfo;
        }

        //---------------------------------------------------------------------------------------------
        public void WSSend()
        {

            HopDongInfo hdInfo = ConvertToHopDongInfo();
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            this.SoTiepNhan=kdt_gc.HopDong_Send(hdInfo);
            this.NgayTiepNhan = DateTime.Today;
            this.TrangThaiXuLy = 0;
            this.InsertUpdate();
        }

        public static void WSSend(HopDongCollection collection)
        {
            HopDongInfo[] hdInfo=new HopDongInfo[collection.Count];
            int i=0;
            foreach (HopDong hd in collection)
            {
                HopDongInfo info=hd.ConvertToHopDongInfo();
                hdInfo[i++]=info;
            }
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            hdInfo = kdt_gc.HopDong_Sends(hdInfo);
            i=0;
            foreach (HopDong hd in collection)
            {
                hd.SoTiepNhan = hdInfo[i++].SoTiepNhan;
                hd.TrangThaiXuLy = 0;
                hd.NgayTiepNhan = DateTime.Today;
                hd.InsertUpdate();
            }
        }
        //---------------------------------------------------------------------------------------------
        //huy mot hop dong
        public bool WSCancel()
        {
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            bool kq=kdt_gc.HopDong_Cancel(this.SoTiepNhan,this.MaHaiQuan,(short)this.NgayTiepNhan.Year);
            this.TrangThaiXuLy = -1;
            this.SoTiepNhan = 0;
            this.NgayTiepNhan = new DateTime(1900, 1, 1);
            this.InsertUpdate();
            return kq;
        }

        public static void WSCancelS(HopDongCollection collection)
        {
            try
            {
                KDT_GC_Service kdt_gc = new KDT_GC_Service();
                HopDongInfo[] hdCollection = new HopDongInfo[collection.Count];
                int i = 0;
                foreach (HopDong hd in collection)
                {
                    HopDongInfo infoHD = new HopDongInfo();
                    infoHD.SoTiepNhan = hd.SoTiepNhan;
                    infoHD.NamTiepNhan = hd.NgayTiepNhan.Year;
                    infoHD.MaHaiQuan = hd.MaHaiQuan;
                    hdCollection[i++] = infoHD;
                }
                kdt_gc.HopDong_Cancels(hdCollection);
                foreach (HopDongInfo hd in hdCollection)
                {
                    collection[i++].isOnServer = hd.TrangThaiXL;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        //---------------------------------------------------------------------------------------------
        //lay thong tin mot hop dong(tat ca thong tin : sp,npl...)
        public bool WSRequest()
        {
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            HopDongInfo hdInfo= kdt_gc.HopDong_DownLoad(this.SoTiepNhan,this.MaHaiQuan,(short)this.NgayTiepNhan.Year);
             bool ret=true;			
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {					                        
                    {             
                    #region Hợp đồng
                        this.TrangThaiXuLy = hdInfo.TrangThaiXL;
                        this._MaDaiLy = hdInfo.MaDaiLy;
                        this._NgayDangKy = hdInfo.NgayDangKy;
                        this._NgayGiaHan = hdInfo.NgayGiaHan;
                        if (_NgayGiaHan.Year < 1900)
                            _NgayGiaHan = new DateTime(1900, 1, 1);
                        this._NgayHetHan = hdInfo.NgayHetHan;
                        this._NgayKy = hdInfo.NgayKy;
                        //this._NgayTiepNhan = hdInfo.NgayTiepNhan;
                        this._NguyenTe_ID = hdInfo.NguyenTe_ID;
                        this._NuocThue_ID = hdInfo.NuocThue_ID;
                        this._SoHopDong = hdInfo.SoHopDong;
                        this._TriGiaGiaCong = hdInfo.TriGiaGiaCong;
                        this.InsertUpdateTransaction(transaction);
                        if(TrangThaiXuLy==1)
                        {
                            Company.BLL.GC.HopDong hdDuyet=new Company.BLL.GC.HopDong();
                            hdDuyet.MaDoanhNghiep=this.MaDoanhNghiep;
                            hdDuyet.MaHaiQuan=this.MaHaiQuan;
                            hdDuyet.NgayGiaHan=this.NgayGiaHan;
                            hdDuyet.NgayHetHan=this.NgayHetHan;
                            hdDuyet.NgayKy=this.NgayKy;
                            hdDuyet.NguyenTe_ID=this.NguyenTe_ID;
                            hdDuyet.NuocThue_ID=this.NuocThue_ID;
                            hdDuyet.SoHopDong=this.SoHopDong;
                            hdDuyet.TrangThaiXuLy=1;
                            hdDuyet.InsertTransaction(transaction);
                        }
                    #endregion Hợp đồng
                        //xoa thong tin ve hang hoa cu cua hop dong
                        NhomSanPham.DeleteByHopDongTransaction(transaction,this.ID);
                        NguyenPhuLieu.DeleteByHopDongTransaction(transaction, this.ID);
                        ThietBi.DeleteByHopDongTransaction(transaction, this.ID);
                        SanPham.DeleteByHopDongTransaction(transaction, this.ID);
                        //cap nhat lai thong tin hang hoa ve nhom sp
                        NhomSanPhamCollection nhomspCollection = new NhomSanPhamCollection();
                        foreach(NhomSanPhamInfo entity in hdInfo.NhomSanPhamGiaCong)
                        {
                            NhomSanPham nhomsp = new NhomSanPham();
                            nhomsp.GiaGiaCong = entity.GiaGiaCong;
                            nhomsp.HopDong_ID = this.ID;
                            nhomsp.MaSanPham = entity.MaNhomSP;
                            nhomsp.SoLuong = entity.SoLuong;
                            nhomsp.STTHang = entity.STTHang;
                            nhomsp.TenSanPham = Company.BLL.DuLieuChuan.NhomSanPham.getTenSanPham(entity.MaNhomSP);
                            nhomsp.InsertTransaction(transaction);
                            //neu duyet thi chuyen sang phan he GC
                            if (TrangThaiXuLy == 1)
                            {
                                Company.BLL.GC.NhomSanPham nhomspDuyet = new Company.BLL.GC.NhomSanPham();
                                nhomspDuyet.Ma = entity.MaNhomSP;
                                nhomspDuyet.MaDoanhNghiep = entity.DVGC;
                                nhomspDuyet.MaHaiQuan = entity.MaHaiQuan;
                                nhomspDuyet.NgayKy = entity.NgaykyHopDong;
                                nhomspDuyet.SoHopDong = entity.SoHopDong;
                                nhomspDuyet.SoLuongDangKy = entity.SoLuong;
                                nhomspDuyet.Ten = Company.BLL.DuLieuChuan.NhomSanPham.getTenSanPham(entity.MaNhomSP);
                                nhomspDuyet.TriGia = entity.GiaGiaCong;
                                nhomspDuyet.InsertTransaction(transaction);
                            }
                        }                                
                        //cap nhat thong tin nguyen phu lieu                
                        foreach (NguyenPhuLieuInfo entity in hdInfo.NguyenPhuLieuCollection)
                        {
                            NguyenPhuLieu npl = new NguyenPhuLieu();
                            npl.DVT_ID = entity.DVT_ID;
                            npl.HopDong_ID = this.ID;
                            npl.Ma = entity.Ma;
                            npl.MaHS = entity.MaHS;
                            npl.SoLuongDangKy = entity.SoLuongDangKy;
                            npl.STTHang = entity.STTHang;
                            npl.Ten = entity.Ten;
                            npl.InsertTransaction(transaction);
                            //neu da duyet thi cap nhat sang phan he GC
                            if (TrangThaiXuLy == 1)
                            {
                                Company.BLL.GC.NguyenPhuLieu nplDuyet = new Company.BLL.GC.NguyenPhuLieu();
                                nplDuyet.DVT_ID = entity.DVT_ID;
                                nplDuyet.Ma = entity.Ma;
                                nplDuyet.MaDoanhNghiep = entity.MaDoanhNghiep;
                                nplDuyet.MaHaiQuan = entity.MaHaiQuan;
                                nplDuyet.MaHS = entity.MaHS;
                                nplDuyet.NgayKy = entity.NgayKy;
                                nplDuyet.SoHopDong = entity.SoHopDong;
                                nplDuyet.Ten = entity.Ten;
                                nplDuyet.SoLuongDangKy = entity.SoLuongDangKy;
                                nplDuyet.InsertTransaction(transaction);
                            }
                        }
                        //cap nhat thong tin thiet bi
                        foreach (ThietBiInfo entity in hdInfo.ThietBiCollection)
                        {
                            ThietBi tb = new ThietBi();
                            tb.DonGia = entity.DonGia;
                            tb.DVT_ID = entity.DVT_ID;
                            tb.HopDong_ID = this.ID;
                            tb.Ma = entity.Ma;
                            tb.MaHS = entity.MaHS;
                            tb.NguyenTe_ID = entity.NguyenTe_ID;
                            tb.NuocXX_ID = entity.NuocXX_ID;
                            tb.SoLuongDangKy = entity.SoLuong;
                            tb.STTHang = entity.STTHang;
                            tb.Ten = entity.Ten;
                            tb.TinhTrang = entity.TinhTrang;
                            tb.TriGia = entity.TriGia;
                            tb.InsertTransaction(transaction);
                            //neu duyet thi chuyen sang phan he GC
                            if (TrangThaiXuLy == 1)
                            {
                                Company.BLL.GC.ThietBi tbDuyet= new Company.BLL.GC.ThietBi();
                                tbDuyet.DonGia = entity.DonGia;
                                tbDuyet.DVT_ID = entity.DVT_ID;
                                tbDuyet.Ma = entity.Ma;
                                tbDuyet.MaDoanhNghiep = entity.MaDoanhNghiep;
                                tbDuyet.MaHaiQuan = entity.MaHaiQuan;
                                tbDuyet.MaHS = entity.MaHS;
                                tbDuyet.NgayKy = entity.NgayKy;
                                tbDuyet.NguyenTe_ID = entity.NguyenTe_ID;
                                tbDuyet.NuocXX_ID = entity.NuocXX_ID;
                                tbDuyet.SoHopDong = entity.SoHopDong;
                                tbDuyet.SoLuongDangKy = entity.SoLuong;
                                tbDuyet.Ten = entity.Ten;
                                tbDuyet.TinhTrang = entity.TinhTrang;
                                tbDuyet.TriGia = entity.TriGia;
                                tbDuyet.InsertTransaction(transaction);
                            }
                        }
                        foreach (SanPhamInfo entity in hdInfo.SanPhamCollection)
                        {
                            SanPham sp = new SanPham();
                            sp.DVT_ID = entity.DVT_ID;
                            sp.HopDong_ID = this.ID;
                            sp.Ma = entity.Ma;
                            sp.MaHS = entity.MaHS;
                            sp.SoLuongDangKy = entity.SoLuongDangKy;
                            sp.STTHang = entity.STTHang;
                            sp.Ten = entity.Ten;
                            sp.NhomSanPham_ID = NhomSanPham.Load(entity.NhomSanPhamGiaCong, this.ID, transaction).ID;
                            sp.InsertTransaction(transaction);
                            //neu duyet thi chuyen sang phan he GC
                            if (TrangThaiXuLy == 1)
                            {
                                Company.BLL.GC.SanPham spDuyet = new Company.BLL.GC.SanPham();
                                spDuyet.DVT_ID = entity.DVT_ID;
                                spDuyet.Ma = entity.Ma;
                                spDuyet.MaDoanhNghiep = entity.MaDoanhNghiep;
                                spDuyet.MaHaiQuan = entity.MaHaiQuan;
                                spDuyet.MaHS = entity.MaHS;
                                spDuyet.NgayKy = entity.NgayKy;
                                spDuyet.NhomSanPham = entity.NhomSanPhamGiaCong;
                                spDuyet.SoHopDong = entity.SoHopDong;
                                spDuyet.SoLuongDangKy = entity.SoLuongDangKy;
                                spDuyet.Ten = entity.Ten;
                                spDuyet.InsertTransaction(transaction);
                            }
                        }
                        
                    }                                        
                    transaction.Commit();
                }
                catch(Exception ex)
                {                    
                    transaction.Rollback();
                    throw ex;
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
        }

        //lay ve trang thai tren hai quan cua nhieu hop dong
        public static void WSReQuestListHopDong(HopDongCollection listHD)
        {
            try
            {
                KDT_GC_Service kdt_gc = new KDT_GC_Service();
                HopDongInfo[] hdCollection = new HopDongInfo[listHD.Count];
                int i = 0;
                foreach (HopDong hd in listHD)
                {
                    HopDongInfo infoHD = new HopDongInfo();
                    infoHD.SoTiepNhan = hd.SoTiepNhan;
                    infoHD.NamTiepNhan = hd.NgayTiepNhan.Year;
                    infoHD.MaHaiQuan = hd.MaHaiQuan;
                    hdCollection[i++] = infoHD;
                }
                hdCollection = kdt_gc.HopDong_Request(hdCollection);                
                i=0;
                foreach (HopDongInfo hd in hdCollection)
                {
                    listHD[i++].isOnServer = hd.TrangThaiXL;
                }                               
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }

        //huy nhieu hop dong
        
        #endregion
    }	
}